package com.yansl.http.autoconfig;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

import com.yansl.http.annotation.EnableHttp;

import cn.hutool.core.util.StrUtil;

/**
 * @description: 注册容器
 * @author: eshengtai
 * @create: 2022-12-17 10:38
 */
public class HttpBeanRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
                    BeanNameGenerator importBeanNameGenerator) {
        // 获取启动器目录
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableHttp.class.getName()));
        String scanPackage = annoAttrs.getString("scanPackage");
        if (StrUtil.isBlank(scanPackage)) {
            scanPackage = ClassUtils.getPackageName(importingClassMetadata.getClassName());
        }

        // 获取扫描类
        HttpScan scan = new HttpScan(registry);
        scan.doScan(scanPackage.split(","));

    }

}
